<!-- 登录页面：login.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>登录 - 日记本</title>
    <meta charset="UTF-8">
    <style>
        body {
            font-family: "Helvetica Neue", Arial, sans-serif;
            background-color: #f4f6f9;
            display: flex;
            height: 100vh;
            align-items: center;
            justify-content: center;
        }

        .login-container {
            background: white;
            padding: 40px 30px;
            border-radius: 12px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
            width: 300px;
        }

        h1 {
            text-align: center;
            margin-bottom: 24px;
            color: #333;
        }

        label {
            display: block;
            margin-top: 12px;
            font-weight: bold;
        }

        input {
            width: 100%;
            padding: 8px;
            margin-top: 4px;
            box-sizing: border-box;
            border-radius: 6px;
            border: 1px solid #ccc;
        }

        button {
            margin-top: 20px;
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            font-weight: bold;
            border: none;
            border-radius: 6px;
            cursor: pointer;
        }

        button:hover {
            background-color: #45a049;
        }

        .error {
            color: red;
            margin-top: 12px;
            text-align: center;
        }

        .register-btn {
            margin-top: 10px;
            background-color: #2196F3;
        }

        .register-btn:hover {
            background-color: #1976D2;
        }
    </style>
</head>
<body>
<div class="login-container">
    <h1>日记本登录</h1>
    <form id="login-form">
        <label for="username">用户名</label>
        <input type="text" id="username" name="username" required>

        <label for="password">密码</label>
        <input type="password" id="password" name="password" required>

        <button type="submit">登录</button>
        <button type="button" class="register-btn" onclick="location.href='/view/register'">注册</button>

        <div id="error-msg" class="error"></div>
    </form>
</div>

<script>
    document.getElementById("login-form").addEventListener("submit", async function (e) {
        e.preventDefault();
        const username = document.getElementById("username").value.trim();
        const password = document.getElementById("password").value.trim();
        const errorMsg = document.getElementById("error-msg");
        errorMsg.textContent = "";

        if (!username || !password) {
            errorMsg.textContent = "请输入用户名和密码";
            return;
        }

        try {
            const response = await fetch("/auth/login", {
                method: "POST",
                headers: { "Content-Type": "application/json" },
                body: JSON.stringify({ username, password })
            });

            if (response.ok) {
                const data = await response.json();
                localStorage.setItem("token", data.token);
                location.href = "/view/home";
            } else {
                const error = await response.text();
                errorMsg.textContent = "登录失败：" + error;
            }
        } catch (err) {
            errorMsg.textContent = "网络错误，请稍后重试";
        }
    });
</script>
</body>
</html>
